//
//  UITableViewController+HDChangeName.m
//  app
//
//  Created by D on 15/4/14.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDChangeName.h"
#import "HDUserInforHttpRequestManger.h"

@implementation HDChangeName

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改昵称";
    self.changeName.delegate = self;
    self.changeName.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"nickname"];
    
    UIButton * rightBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT ox:-20 fy:UB_TOP],
                                                                   [self.ubo ow:40 oh:40])];
    
    [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[XCommon hexStringToColor:kColor_gray_text] forState:UIControlStateNormal];
    [rightBtn.titleLabel setFont:JKAppGlobleFontMenu];
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightBtn addTarget:self action:@selector(save:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];

    
    self.result_name = [JKRemoteResult target:self action:@selector(changeNickname:)];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
}

-(void)save:(UIButton*)sender
{
     [self.changeName resignFirstResponder];
    self.progress_ = [[MBProgressHUD alloc]initWithView:self.view];
    [self.view addSubview:self.progress_];
    [self.view bringSubviewToFront:self.progress_];
    self.progress_.delegate = self;
    [self.progress_ show:YES];
    
    HDUserInforHttpRequestManger * httpManager = [HDUserInforHttpRequestManger shareUserInforHttpRequestManger];
    NSDictionary * params = @{@"nickname":self.changeName.text};
    [httpManager changeUserInfoWithParams:params reuslt:self.result_name];
    
}
-(void)changeNickname:(JKRemoteResult*)rt
{
    if (self.progress_) {
        [self.progress_ removeFromSuperview];
        self.progress_ = nil;
    }
    

    if (rt.failure) {
        UIAlertView * av = [[UIAlertView alloc]initWithTitle:nil message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [self.view addSubview:av];
        [av show];
        
        return;
    }
    [[NSUserDefaults standardUserDefaults] setObject:self.changeName.text forKey:@"nickname"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [alertView removeFromSuperview];
}

@end
